﻿using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class PaneRecord : WorksheetLevelRecord
    {
        private ushort colLeft;
        private PaneType pnnAcct;
        private ushort rwTop;
        private ushort x;
        private ushort y;

        public PaneRecord(Worksheet worksheet)
        {
            Contract.Requires(worksheet != null);
            Contract.Requires(worksheet.View != null);
            Contract.Requires(worksheet.View.Pane != null);
            WorksheetPane pane = worksheet.BottomRightPane;
            pnnAcct = PaneType.BottomRight;
            if (worksheet.View.Pane.State == PaneState.Frozen)
            {
                y = (ushort) ((ushort) worksheet.View.Pane.RowCount - 1);
                x = (ushort) ((ushort) worksheet.View.Pane.ColumnCount - 1);
                rwTop = (ushort) pane.Top;
                colLeft = (ushort) pane.Left;
            }
        }

        public PaneRecord()
        {
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Pane; }
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            x = context.ReadUInt16();
            y = context.ReadUInt16();
            rwTop = context.ReadUInt16();
            colLeft = context.ReadUInt16();
            pnnAcct = (PaneType) context.ReadByte();
            byte reserved = context.ReadByte();
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            Window2Record window2Record = context.LastWindow2Record;
            if (window2Record == null)
                return;
            if (window2Record.FFrozenRt)
            {
                if (window2Record.FFrozenNoSplit)
                {
                    Debug.WriteLine("throwing away frozen no split");
                }
                int? row = y + context.Worksheet.TopLeftPane.Top;
                int? col = x + context.Worksheet.TopLeftPane.Left;
                switch (pnnAcct)
                {
                    case PaneType.BottomLeft:
                        col = null;
                        context.Worksheet.FreezePanes(row, col);
                        context.Worksheet.BottomLeftPane.Top = rwTop;
                        context.Worksheet.BottomLeftPane.Left = colLeft;
                        break;
                    case PaneType.TopRight:
                        row = null;
                        context.Worksheet.FreezePanes(row, col);
                        context.Worksheet.TopRightPane.Top = rwTop;
                        context.Worksheet.TopRightPane.Left = colLeft;
                        break;
                    case PaneType.TopLeft:
                        col = null;
                        row = null;
                        break;
                    case PaneType.BottomRight:
                        context.Worksheet.FreezePanes(row, col);
                        context.Worksheet.BottomRightPane.Top = rwTop;
                        context.Worksheet.BottomRightPane.Left = colLeft;
                        break;
                }
                return;
            }
            {
                context.Worksheet.SplitPanes(rwTop, colLeft);
            }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write(x);
            context.Write(y);
            context.Write(rwTop);
            context.Write(colLeft);
            context.Write((byte) pnnAcct);
            context.Write((byte) 0);
        }
    }
}